package com.zm.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.zm.bean.BatJson;
import com.zm.service.DemoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping(value = "/demo")
public class DemoController {

    @Resource
    DemoService demoService;


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    @ResponseBody
    public List<BatJson> demo() {
        String url = "http://mongo.yiqipower.com/api_mongo.php/Info/log/9232211280513008/2019-05-08?door=8&out=json";
        String result = demoService.getForString(url);

        Type type = new TypeToken<List<BatJson>>() {
        }.getType();

        List<BatJson> batJson = new Gson().fromJson(result, type);

        for (int i = 0; i < batJson.size(); i++) {
            List<BatJson.InfoBean> infoBeans = batJson.get(i).getInfo();

            for (int j = 0; j < infoBeans.size(); j++) {

                BatJson.InfoBean infoBean = infoBeans.get(j);
                infoBean.battery ="No:"+ infoBean.battery;
                infoBean.timeFormat = "时间:"+sdf.format(new Date(infoBean.time * 1000));
                infoBeans.set(j, infoBean);
            }
        }

        return batJson;

    }

}
